/**
 * 
 */
package cn.mxz.robots.province;

import java.io.File;
import java.io.IOException;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import sun.misc.BASE64Encoder;
import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 山西
 * 排名:完成
 * 去年排名:完成
 * 
 * @author chenmin<chenmin@mouge.wang>
 * @since 2015-06-25
 */
public class Shanxi extends Exam implements IRank
{
	private BASE64Encoder base64Encoder = new BASE64Encoder();

	public Shanxi(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{

		Student student = new Student();
		student.setStudentId("15141102151460");
		student.setStudentPwd("FengHua15534392884");
		student.setType("l");
		Shanxi guangxi = new Shanxi(student);
		System.out.println(guangxi.login(""));
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://gkpt.sxkszx.cn/Ashx/GkIndex.ashx", mSha1);
		loginRequest.addHeader("Referer", "http://gkpt.sxkszx.cn/gkindex.aspx");
		loginRequest.addParams("username", mStudent.getStudentId());
		loginRequest.addParams("password", base64Encoder.encode(mStudent.getStudentPwd().getBytes()).substring(0, 11)
				+ "=");
		String loginHtml = curlPost(loginRequest);
	
		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("success"))
		{
			Request lastRequest = new Request("http://gkpt.sxkszx.cn/Ashx/CxAshx/Cxcj.ashx", mSha1);

			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(curlPost(lastRequest)));
		}
		else if (loginHtml.contains("操作失败"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		JSONObject result = JSONObject.fromObject(parseHtml);

		JSONObject json = new JSONObject();
		json.put("exam_id", result.get("ksh"));
		json.put("name", result.get("kxm"));
		json.put("ranking", getRank(23, regexMatch(result.get("hj").toString(), "(.*)\\."), mStudent.getType()));
		json.put("province", "山西省");

		JSONObject score = new JSONObject();
		score.put(
				"语文单科成绩",
				Math.round(Float.parseFloat(result.get("kyw").toString())
						+ Float.parseFloat(result.get("zyw").toString())));
		score.put("语文主观题", Math.floor(Float.parseFloat(result.get("zyw").toString())));
		score.put("语文客观题", Math.floor(Float.parseFloat(result.get("kyw").toString())));
		score.put(
				"数学单科成绩",
				Math.round(Float.parseFloat(result.get("ksx").toString())
						+ Float.parseFloat(result.get("zsx").toString())));
		score.put("数学主观题", Math.floor(Float.parseFloat(result.get("zsx").toString())));
		score.put("数学客观题", Math.floor(Float.parseFloat(result.get("ksx").toString())));
		score.put(
				"外语单科成绩",
				Math.round(Float.parseFloat(result.get("kwybs").toString())
						+ Float.parseFloat(result.get("zwybs").toString())));
		score.put("外语主观题", Math.floor(Float.parseFloat(result.get("kwybs").toString())));
		score.put("外语客观题", Math.floor(Float.parseFloat(result.get("zwybs").toString())));
		score.put(
				"综合单科成绩",
				Math.round(Float.parseFloat(result.get("kzh").toString())
						+ Float.parseFloat(result.get("zzh").toString())));
		score.put("综合主观题", Math.floor(Float.parseFloat(result.get("kzh").toString())));
		score.put("综合客观题", Math.floor(Float.parseFloat(result.get("zzh").toString())));
		score.put("外语听力", Math.floor(Float.parseFloat(result.get("wytl").toString())));
		score.put("照顾分", Math.floor(Float.parseFloat(result.get("zgf").toString())));
		score.put("文化总分", Math.floor(Float.parseFloat(result.get("hj").toString())));
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		File file = new File("/home/chenmin/Desktop/test.html");

		Document test;
		try
		{
			test = Jsoup.parse(file, "UTF-8");
			Elements trs = test.select("table").get(2).select("tr");

			for (int i = 1; i < trs.size(); i++)
			{
				for (int j = 0; j < 6; j += 3)
				{
					Integer score = Integer.valueOf(trs.get(i).select("td").get(j).text());
					Integer counter = Integer.valueOf(trs.get(i).select("td").get(j + 1).text());
					Integer ranking = Integer.valueOf(trs.get(i).select("td").get(j + 2).text());
					System.out.println(score + ":" + counter + ":" + ranking);
					GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
					GaoKaoProvince province = provinceService.get(23 + 31);
					GaoKaoRankService rankService = new GaoKaoRankService();

					GaoKaoRank rank = new GaoKaoRank();
					rank.setScore(score);
					rank.setCount(counter);
					rank.setType("l");
					rank.setRanking(ranking);
					rank.setGaoKaoProvince(province);
					rankService.save(rank);
				}
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	@Override
	public void lastYearRank()
	{
		//理 http://www.gxeduw.com/news/2014/107177.html
		//文 http://www.gxeduw.com/news/2014/107176.html
		Request request = new Request("http://www.gxeduw.com/news/2014/107176.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			if(trs.get(i).select("td").get(0).text().replaceAll(" ", "").contains("分数"))
				continue;
			
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(23);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(WEN_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}

	}
}
